﻿using System;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using luck_draw.common;
using Brush = System.Windows.Media.Brush;
using Color = System.Windows.Media.Color;
using Microsoft.DirectX.AudioVideoPlayback;

namespace luck_draw.control
{

	/// <summary>
	/// DrawBtn.xaml 的交互逻辑
	/// </summary>
	[ToolboxItem(true)]
	public partial class DrawBtn : UserControl
	{

		/// <summary>
		/// 定义Image的源
		/// </summary>
		public ImageSource ImgSource
		{
			get => img.Source;
			set => img.Source = value;
		}

		/// <summary>
		/// 定义控件的背景色
		/// </summary>
		public new Brush Background
		{
			get => canvas.Background;
			set
			{
				canvas.Background = value;
				if (!inited)
				{
					sourceBrush = value;
					inited = true;
				}
			}
		}

		/// <summary>
		/// 定义控件的边框色
		/// </summary>
		public new  Brush BorderBrush
		{
			get => base.BorderBrush;
			set => base.BorderBrush = value;
		}

		/// <summary>
		/// 定义奖励文本
		/// </summary>
		public string Text
		{
			get => tbx.Text;
			set => tbx.Text = value;
		}

		/// <summary>
		/// 指示该奖励格子是否被打开
		/// </summary>
		public bool IsOpen { get; private set; }

		/// <summary>
		/// 文本框的高度
		/// </summary>
		public double TbxHeight
		{
			get => tbx.Height;
			set => tbx.Height = value;
		}

		private PowerType _power;

		/// <summary>
		/// 该奖励格子的属性
		/// </summary>
		public PowerType Power
		{
			get => _power;
			set
			{
				_power = value;
				this.BorderBrush = new SolidColorBrush(TransferColor());
			}
		}

		private bool inited = false;
		private Brush sourceBrush = null;
		public DrawBtn()
		{
			InitializeComponent();
		}

		private void UserControl_Initialized(object sender, EventArgs e)
		{
			sourceBrush = this.Background;
		}

		/// <summary>
		/// 根据属性得到该控件的主颜色
		/// </summary>
		/// <returns></returns>
		public Color TransferColor()
		{
			switch (_power)
			{
				case PowerType.ELECTRIC: return Colors.Purple;
				case PowerType.FIRE: return Colors.Red;
				case PowerType.GRASS: return Colors.GreenYellow;
				case PowerType.SOIL: return Colors.DarkGoldenrod;
				case PowerType.WATER: return Colors.LightSkyBlue;
			}

			return Colors.Transparent;
		}

		/// <summary>
		/// 该控件被选择到时执行的动画
		/// </summary>
		/// <param name="time">动画执行的时间</param>
		public async void SelectedAnimate(int time)
		{
			Audio audio = new Audio("sound/"+Config.SelectMusicName);
			Background = new SolidColorBrush(TransferColor());
			if(Config.StartSelectMusic) audio.Play();
			audio.Ending += (sender, e) =>
			{
				Task.Run(async() =>
				{
					await Task.Delay(50);
					audio.Dispose();
				});
			};
			await Task.Delay(time);
			Background = sourceBrush;
		}

		/// <summary>
		/// 选择完成后，显示奖励文字的动画
		/// </summary>
		/// <param name="timespan">动画帧时间</param>
		/// <param name="completed">动画执行完成的回调</param>
		public async void SelectCompleteAnimate(int timespan,Action<string> completed)
		{
			IsOpen = true;
			for (int i = 0; i <= this.Height - 3; i++)
			{
				TbxHeight = i;
				await Task.Delay(timespan);
			}
			completed?.Invoke(Text);
		}

	}
}
